﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace HtmlExtractor
{
    public static class SegmentRegex
    {
        
        public static SegmentCollection Match(string input, string pattern, RegexOptions options, bool processMultiple)
        {            
            return new SegmentCollection(Operation.Match, input, pattern, null, options, processMultiple);
        }
        public static string Replace(string input, string pattern, string replace, RegexOptions options)
        {
            
            Regex re = new Regex(pattern, options);
            string replaced = re.Replace(input, replace);
            return replaced;
        }
        /// <summary>
        /// 去掉所有零宽度匹配和空匹配的结果
        /// </summary>
        /// <param name="input"></param>
        /// <param name="pattern"></param>
        /// <param name="options"></param>
        /// <param name="processMultiple"></param>
        /// <returns></returns>
        public static List<string> Split(string input, string pattern, RegexOptions options, bool processMultiple)
        {
            Regex re = new Regex(pattern, options);
            return re.Split(input).Where(s=>!string.IsNullOrEmpty(s)).ToList();
        }
       
        public static bool IsMatch(string input, string pattern, RegexOptions options)
        {
           // todo 其他需要转义
            //if (pattern.Contains("&nbsp"))
            //{
            //    pattern = pattern.UnescapeXML();
            //}
            SegmentCollection sc = Match(input, pattern.UnescapeXML(), options, false);
            if (sc.MatchCount == 1)
                return true;
            else
                return false;
        }
        public static bool TryRequiredSingleMatch(string input, string pattern, RegexOptions options, out string matchString)
        {
            matchString="";
            SegmentCollection segments=Match(input, pattern,options,true);
            if (segments.MatchCount != 1)
                throw new Exception();
            foreach (Segment s in segments)
            {
                if (s.Match != null)
                {
                    matchString = s.Match.OriginalValue;
                    return true;
                }
            }
            return false;
           
        }

        public static bool TryRequiredSingleGroupMatch(string input, string pattern, RegexOptions options, out string matchString)
        {
            matchString = "";
            SegmentCollection segments = Match(input, pattern, options, true);
            if (segments.MatchCount != 1)
                throw new Exception();
            foreach (Segment s in segments)
            {
                SegmentMatch match = s.Match;
                if (match != null)
                {
                    foreach (SegmentMatchGroup group in match.MatchGroups)
                    {
                        matchString= group.Value;
                        return true;
                    }
                   
                }
            }
            return false;
        }
    }
}
